/*************************************************************************
> File Name: send_broadcast.c
> Author: hzy
> Created Time: 2023-09-20 11:39:32
> Description:
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc, char const *argv[])
{
    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
    {
        perror("socket");
        return -1;
    }
    // 设置套接字属性为广播
    int optval = 1;
    int ret = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
    if (ret < 0)
    {
        perror("setsockopt");
        return -1;
    }

    // 发送广播
    struct sockaddr_in send_addr;
    send_addr.sin_family = AF_INET;
    send_addr.sin_port = htons(10086);
    send_addr.sin_addr.s_addr = inet_addr("10.7.178.255");

    for (int i = 0; i < 10; i++)
    {
        int ret1 = sendto(sockfd,
                          "你们好吗？",
                          sizeof("你们好吗？"),
                          0,
                          (struct sockaddr *)&send_addr,
                          sizeof(send_addr));
        if (ret1 < 0)
        {
            perror("sendto");
            return -1;
        }
    }

    return 0;
}